﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Configuration;

namespace Incendios0_1.Camera.wpfdatamanager
{
    /// <summary>
    /// Interaction logic for CameraListView.xaml
    /// </summary>
    public partial class CameraListView : Window
    {
        

        Boolean dirty;

        public CameraListView()
        {
            InitializeComponent();
            listView.ItemsSource = CameraManager.CamerasData;
            dirty = false;
        }

        private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (dirty)
                e.Handled = true;
        }

        private void ValidationError(object sender, ValidationErrorEventArgs e)
        {
            dirty = true;
        }

        private void BindingTargetUpdated(object sender, DataTransferEventArgs e)
        {
            dirty = false;
        }

        private void BindingSourceUpdated(object sender, DataTransferEventArgs e)
        {
            dirty = false;
        }

        private void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!dirty)
                CameraManager.SaveCamera();
        }

        private void new_Click(object sender, RoutedEventArgs e)
        {
            if (!dirty)
            {
                CameraData newCamera = new CameraData();
                newCamera.Name = Guid.NewGuid().ToString();
                newCamera.Url = "http://";
                CameraManager.AddCamera(newCamera);
                listView.ItemsSource = CameraManager.CamerasData;
                listView.SelectedIndex = listView.Items.Count - 1;
                listView.SelectedIndex = -1;
            }
        }

        private void save_Click(object sender, RoutedEventArgs e)
        {
            if (!dirty)
                CameraManager.SaveCamera();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (dirty)
                e.Cancel = true;
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            // Configure the message box to be displayed
            /*string messageBoxText = "Do you want to save changes?";
            string caption = "Word Processor";
            MessageBoxButton button = MessageBoxButton.OK;
            MessageBoxImage icon = MessageBoxImage.Warning;
            MessageBox.Show(messageBoxText, caption, button, icon);*/
            CameraData dCamera = listView.SelectedItem as CameraData;
            if (dCamera != null)
                CameraManager.deleteCamera(dCamera);
            listView.ItemsSource = CameraManager.CamerasData;
            listView.SelectedIndex = listView.Items.Count - 1;
            listView.SelectedIndex = -1;
        }
    }
}
